package cqut.gbq.qimo.service.impl;

import cqut.gbq.qimo.entity.Assignment;
import cqut.gbq.qimo.entity.Record;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.entity.Student;
import cqut.gbq.qimo.mapper.AssignmentMapper;
import cqut.gbq.qimo.mapper.CourseMapper;
import cqut.gbq.qimo.service.AssignmentService;
import cqut.gbq.qimo.service.RecordService;
import cqut.gbq.qimo.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;


@Service
public class AssignmentServiceImpl implements AssignmentService {

    @Autowired
    private AssignmentMapper assignmentMapper;
    @Autowired
    private StudentsService studentsService;
    @Autowired
    private RecordService recordService;

    @Override
    public ResBean getAssignmentDetail(Integer aid) {
        List<Student> studentList = assignmentMapper.getAssignmentDetail(aid);

        return ResBean.success("获取学生信息成功",studentList);
    }

    @Override
    public List<Assignment> findAssignmentsByCid(Integer cid) {
        return assignmentMapper.findAssignmentsByCid(cid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResBean createAssignment(Assignment assignment) {

        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = tempDate.format(new java.util.Date());
        assignment.setAssignmentStart(startTime);
//        Integer count = assignmentMapper.createAssignment(assignment.getAssignmentDesc(),startTime, assignment.getAssignmentEnd(),assignment.getAssignmentName(),assignment.getFullScore(),assignment.getCid());
        Integer count = assignmentMapper.createAssignment(assignment);




        Integer aid = assignment.getAid();
        List<Student> studentByCid = studentsService.getStudentByCid(assignment.getCid());
        for (Student item:studentByCid) {
            Record record = new Record();
            record.setAid(aid);
            record.setSid(item.getSid());
            ResBean resBean1 = recordService.insertRecord(record);
        }
        if (count > 0 ){
            return ResBean.success("创建作业成功!");
        }else{
            return ResBean.error("创建作业失败!");
        }
    }

    @Override
    public Integer findAid(Assignment assignment) {
        return assignmentMapper.findAid(assignment);
    }

    @Override
    public ResBean getAssignmentByAid(Integer aid) {
        try {
            Assignment assignment = assignmentMapper.getAssigmentByAid(aid);
            return ResBean.success("获取成功",assignment);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResBean.success("获取失败");

    }

    @Override
    public ResBean editScore(Integer sid, Integer aid, Integer score,Integer ischeck) {

        if (ischeck == 1 ){
            try{
                Integer count =  assignmentMapper.editScore(sid,aid,score);
                return ResBean.success("修改成功!");
            }catch (Exception e){
                e.printStackTrace();
            }
            return ResBean.success("修改失败!");
        }else{
            try {
                Integer count =  assignmentMapper.editScore(sid,aid,score);
                return ResBean.success("批阅成功!");
            }catch (Exception e){
                e.printStackTrace();
            }
            return ResBean.success("批阅失败!");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAssignmentByAid(Integer aid) {
        Integer row = assignmentMapper.deleteAssignmentByAid(aid);
        assignmentMapper.deleteAssignmentAndStudentByAid(aid);
        if (row == 1){
            return true;
        }
        return false;
    }

    @Override
    public Integer editAssignment(Assignment assignment) {
        Integer row = assignmentMapper.editAssignment(assignment);

        return row;
    }
}
